#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;

const int N = 1e5 + 10;
typedef long long ll;
ll arr[N];

int main()
{
	int n; cin >> n;
	for (int i = 1; i <= n; i++) cin >> arr[i];
	sort(arr + 1, arr + n+1);
	ll mid = 1 + (n - 1) / 2;
	ll ret = 0;
	for (int i = 1; i <= n; i++) ret += abs(arr[i] - arr[mid]);
	cout << ret;
	return 0;
}